﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icst.Leaflike.Logging;

namespace Icst.Leaflike.Data
{
    public class Gathering : DataContext<Gathering>
    {
        public int CountGathers(int uid)
        {
            try
            {
                return QueryByUid(uid).Count();
            }
            catch (Exception e)
            {
                Logger.Error("FAILE TO COUNT GATHERS. " + e.Message);
                throw;
            }
        }
        public tree_gather[] FindGathers(int uid, int page, int pagesize)
        {
            try
            {
                return QueryByUid(uid).Skip((page - 1) * pagesize).Take(pagesize).ToArray();
            }
            catch (Exception e)
            {
                Logger.Error("FAILE TO FIND GATHERS with UID= " + uid + ". " + e.Message);
                throw;
            }
        }
        public tree_gather FindGather(long gid)
        {
            try
            {
                return Context.tree_gathers.SingleOrDefault(p => p.gather_id == gid);
            }
            catch (Exception e)
            {
                Logger.Error("FAILE TO FIND GATHER with " + gid + ". " + e.Message);
                throw;
            }
        }
        public tree_gather[] SearchGathers(int uid, int page, int sort, int pagesize)
        {
            try
            {
                switch (sort)
                {
                    case 1: return QueryByUid(uid).OrderBy(p => p.created).Skip((page - 1) * pagesize).Take(pagesize).ToArray();
                    case 2: return QueryByUid(uid).OrderBy(p => p.site).Skip((page - 1) * pagesize).Take(pagesize).ToArray();
                    case 3: return QueryByUid(uid).Where(p => (DateTime.Now - p.created).Minutes < 10).Skip((page - 1) * pagesize).Take(pagesize).ToArray();
                    default: break;
                }
                return QueryByUid(uid).Skip((page - 1) * pagesize).Take(pagesize).ToArray();
            }
            catch (Exception e)
            {
                Logger.Error("FAILE TO SEARCH GATHER with UID= " + uid + ". " + e.Message);
                throw;
            }
        }
        public void InsertGather(tree_gather g) 
        {
            if (g.uid == -1)
            {
                Logger.Info("IGNORE TO INSERT GATHER SINCE UID=-1");
                return;
            }
            try
            {
                Context.tree_gathers.InsertOnSubmit(g);
                Context.SubmitChanges();
            }
            catch (Exception e)
            {
                Logger.Error("FAILE TO INSERT GATHER. " + e.Message);
                throw;
            }
        }
        public void DeleteGather(int gid)
        {
            try
            {
                tree_gather g = FindGather(gid);
                if (g != null)
                {
                    Context.tree_gathers.DeleteOnSubmit(g);
                    Context.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                Logger.Error("FAILE TO DELETE GATHER " + gid + ". " + e.Message);
                throw;
            }
        }
        public void UpdateGather(tree_gather g)
        {
            try
            {
                tree_gather ng = FindGather(g.gather_id);
                if (ng != null)
                {
                    ng.created = g.created;
                    ng.description = g.description;
                    ng.image_link = g.image_link;
                    ng.site = g.site;
                    ng.title = g.title;
                    ng.created = DateTime.Now;
                    Context.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                Logger.Error("FAILE TO UPDATE GATHER " + g.gather_id + ". " + e.Message);
                throw;
            }
        }
        public string Export(string type, string optimize)
        {
            switch (type)
            {
                case "PDF": break;
                case "XPS": break;
            }
            return "http://www.irf.com/product-info/datasheets/data/df.pdf";
        }
        private IQueryable<tree_gather> QueryByUid(int uid)
        {
            return Context.tree_gathers.Where(p => p.uid == uid);
        }
    }
}
